﻿-- AddonFrame
local RaidFramesPlus = CreateFrame("Frame") 

-- local variables
local RaidFramesPlus_ShowBuffs = 0 -- Raidwide toggle status for Debuff/Buff display
local RaidFramesPlus_FontStringArray = {} -- Associated Lookuptable (key : UnitId; value : Array of FontStrings )
local RaidFramesPlus_DebuffBackgroundArray = {} -- Associated Lookuptable (key: UnitId; value : Array of Textures )
 RaidFramesPlus_RaidTargetArray = {} -- Associated Lookuptable (key: UnitId; value : Array of Textures )
local RaidFramesPlus_ReplaceTable = { -- Associated Lookuptable (key: replaceable-token; value : function )
 ["$abs"] = function(UnitId)
	return UnitHealth(UnitId)
end,

 ["$max"] = function(UnitId) 
	return UnitHealthMax(UnitId)
end,

 ["$dif"] = function(UnitId) 
	return UnitHealth(UnitId) - UnitHealthMax(UnitId) 
end,

 ["$abk"] =  function(UnitId) 
    local value = UnitHealth(UnitId)
	if value > 1000 then
		return string.format("%.1fk", value/1000)
	else
		return value
	end
end,
 ["$mak"] =  function(UnitId) 
    local value = UnitHealthMax(UnitId)
	if value > 1000 then
		return string.format("%.1fk", value/1000)
	else
		return value
	end
end,
 ["$difk"] =  function(UnitId) 
    local value = UnitHealthMax(UnitId) - UnitHealth(UnitId)
	if value > 1000 then
		return string.format("-%.1fk", value/1000)
	else
		return value
	end
end,
}

-- functions	
--[[
--  RaidFramesPlus_UnitThreatSituationUpdate 
--  TAKES
--   UnitId (uitype:UNIT)
--  RETURNS
--   nil
--  
--  DESCRIPTION
--   this function lookup the table RaidFramesPlus_FontStringArray with given UnitId parameter to access
--   fontStrings that are associated to the UnitButton in RaidPullouts to color it by the 
--   corresponding Threat Situation.
--]]
local function RaidFramesPlus_UnitThreatSituationUpdate(UnitId)
	local FontStringArray = RaidFramesPlus_FontStringArray[UnitId]
	if FontStringArray == {} or FontStringArray == nil then 
		return 
	end
	for i, fontString in pairs(FontStringArray) do
		local threatState = UnitThreatSituation(UnitId)
		if threatState == 1 then
			_G[fontString]:SetVertexColor(1, 0.75, 0, 1)
		elseif threatState == 2 then
			_G[fontString]:SetVertexColor(1, 0.5, 0, 1)
		elseif threatState == 3 then
			_G[fontString]:SetVertexColor(1, 0, 0, 1)
		else
			_G[fontString]:SetVertexColor(1, 1, 1, 1)
		end
	end
end
--[[
--  RaidFramesPlus_RaidTargetUpdate
--  TAKES
--   nil
--  RETURNS
--   nil
-- 
--  DESCRIPTION
--   scan through the whole raid and get for every raidmember the current RaidTargetIndex to
--   display thiers Icon next the Unitframe
--]]
local function RaidFramesPlus_RaidTargetUpdate()

local pullout = 0
	while(_G["RaidPullout"..pullout+1] ~= nil) do
			-- there is a next RaidPullout so lets use it
			pullout = pullout + 1
			-- now we check for RaidButtons
			local button = 0
			while(_G["RaidPullout"..pullout.."Button"..button+1] ~= nil) do
				button = button + 1
				local iconIndex = GetRaidTargetIndex(_G["RaidPullout"..pullout.."Button"..button].unit)
				if iconIndex then
					SetRaidTargetIconTexture(_G["RaidPullout"..pullout.."Button".. button.. "RaidTargetIcon"],iconIndex)
					_G["RaidPullout"..pullout.."Button".. button.. "RaidTargetIcon"]:Show()
				else
					_G["RaidPullout"..pullout.."Button".. button.. "RaidTargetIcon"]:Hide()
				end
	
			end
	end
end
--[[
--  RaidFramesPlus_PlayerTargetchanged 
--  TAKES
--   nil
--  RETURNS
--   nil
--  
--  DESCRIPTION
--   color name of the target by the corresponding class color (if the flag RaidFramesPlus_ColorizeNames is set)
--]]
local function RaidFramesPlus_PlayerTargetChanged()
	if RaidFramesPlus_ColorizeNames == 1 then
		local classcolor = RAID_CLASS_COLORS[select(2, UnitClass("Target"))] or {r = 1, g = 0.82, b = 0}
		TargetName:SetVertexColor(classcolor.r, classcolor.g, classcolor.b, 1)
	else
		TargetName:SetVertexColor(1, 0.82, 0, 1)
	end
	if RaidFramesPlus_HideTargetFrameNameBackground == 1 then
		TargetFrameNameBackground:Hide()
	else
		TargetFrameNameBackground:Show()
	end
end
--[[
--  RaidFramesPlus_GetDebuffColor 
--  TAKES
--   UnitId (uitype:UNIT)
--  RETURNS
--   r (type:number)
--   g (type:number)
--   b (type:number)
--   a (type:number)
--  
--  DESCRIPTION
--   this function returns the color of the latest debuff from the Unit with the given UnitId-Token.
--]]
local function RaidFramesPlus_GetDebuffColor(UnitId)
	local r,g,b,a
	local latestDebuffType
	if GetCVarBool("showDispelDebuffs") then
		latestDebuffType = select(5, UnitAura(UnitId, 1, "HARMFUL|RAID"))
	else
		latestDebuffType = select(5, UnitAura(UnitId, 1, "HARMFUL"))
	end
	if latestDebuffType ~= nil then
		r = DebuffTypeColor[latestDebuffType].r
		g = DebuffTypeColor[latestDebuffType].g
		b = DebuffTypeColor[latestDebuffType].b
		a = 1
	else
		r = 0
		g = 0
		b = 0
		a = 0
	end
	return r,g,b,a
end
--[[
--  RaidFramesPlus_UnitAura 
--  TAKES
--   UnitId (uitype:UNIT)
--  RETURNS
--   nil
--  
--  DESCRIPTION
--   this function change the color or hide the scoped debuff background's with the corresponding 
--   UnitId by using RaidFramesPlus_GetDebuffColor
--   
--  SEE ALSO
--   RaidFramesPlus_GetDebuffColor
--   RaidFramesPlus_UnitThreatSituationUpdate
--]]
local function RaidFramesPlus_UnitAura(UnitId)
	-- check the probably populated Array for an associated texture
	local backgroundArray = RaidFramesPlus_DebuffBackgroundArray[UnitId]
	if backgroundArray == nil then
		return
	end
	for i, background in pairs(backgroundArray) do
		if background then
			
			_G[background]:SetVertexColor(RaidFramesPlus_GetDebuffColor(UnitId))
		end
	end
end
--[[
--   RaidFramesPlus_PartyMemberChanged
--]]
local function RaidFramesPlus_PartyMemberChanged()
if RaidFramesPlus_ColorizeNames == 1 then
		local classcolor = RAID_CLASS_COLORS[select(2, UnitClass("Player"))]
		PlayerName:SetVertexColor(classcolor.r, classcolor.g, classcolor.b, 1)
	else
		PlayerName:SetVertexColor(1, 0.82, 0, 1)
	end
	if RaidFramesPlus_ColorizeNames == 1 then
		local classcolor = RAID_CLASS_COLORS[select(2, UnitClass("Party1"))] or {r = 1, g = 0.82, b = 0}
		PartyMemberFrame1Name:SetVertexColor(classcolor.r, classcolor.g, classcolor.b, 1)
	else
		PartyMemberFrame1Name:SetVertexColor(1, 0.82, 0, 1)
	end
	if RaidFramesPlus_ColorizeNames == 1 then
		local classcolor = RAID_CLASS_COLORS[select(2, UnitClass("Party2"))] or {r = 1, g = 0.82, b = 0}
		PartyMemberFrame2Name:SetVertexColor(classcolor.r, classcolor.g, classcolor.b, 1)
	else
		PartyMemberFrame2Name:SetVertexColor(1, 0.82, 0, 1)
	end
	if RaidFramesPlus_ColorizeNames == 1 then
		local classcolor = RAID_CLASS_COLORS[select(2, UnitClass("Party3"))] or {r = 1, g = 0.82, b = 0}
		PartyMemberFrame3Name:SetVertexColor(classcolor.r, classcolor.g, classcolor.b, 1)
	else
		PartyMemberFrame3Name:SetVertexColor(1, 0.82, 0, 1)
	end
	if RaidFramesPlus_ColorizeNames == 1 then
		local classcolor = RAID_CLASS_COLORS[select(2, UnitClass("Party4"))] or {r = 1, g = 0.82, b = 0}
		PartyMemberFrame4Name:SetVertexColor(classcolor.r, classcolor.g, classcolor.b, 1)
	else
		PartyMemberFrame4Name:SetVertexColor(1, 0.82, 0, 1)
	end
end 
--[[
--  RaidFramesPlus_RaidRoasterUpdate
--  TAKES
--   nil
--  RETURNS
--   nil
--  
--  DESCRIPTION
--   this function adds newly added RaidPullouts (and buttons) to scoping-tables indexed with 
--   UnitId. Also it colorize Partymember names by thier class color if flag 
--   RaidFramesPlus_ColorizeNames is set) and hide TargetFrameNameBackground for better reading 
--   the class-colored Targetname if flag RaidFramesPlus_HideTargetFrameNameBackground is set
--]]
local function RaidFramesPlus_RaidRosterUpdate()

	-- first we depopulate the list
	RaidFramesPlus_DebuffBackgroundArray = {}
	RaidFramesPlus_FontStringArray = {}
	
	-- we will now populate the RaidFramesPlus_FontStringArray
	-- first, we check for a RaidPullout
	local pullout = 0
	while(_G["RaidPullout"..pullout+1] ~= nil) do
		-- there is a next RaidPullout so lets use it
		pullout = pullout + 1
		-- now we check for RaidButtons
		local button = 0
		while(_G["RaidPullout"..pullout.."Button"..button+1] ~= nil) do
			-- there is a next RaidPulloutButton so lets use it
			button = button + 1
			btn = _G["RaidPullout"..pullout.."Button"..button]
			-- create a backgroundlayer for Debuff Indicator
			local background = _G["RaidPullout"..pullout.."Button"..button.."Debuff"] or 
								btn:CreateTexture("RaidPullout"..pullout.."Button"..button.."Debuff", "BACKGROUND")
			background:SetTexture("Interface\\AddOns\\RaidFramesPlus\\DebuffBorder")
			background:SetBlendMode("ADD")
			background:SetWidth(82)
			background:SetHeight(36)
			background:SetPoint("TOPLEFT", btn, "TOPLEFT", -6, 6)
			background:SetTexCoord(0.0, 0.640625, 0, 0.625)
			
			background:SetVertexColor(RaidFramesPlus_GetDebuffColor(btn.unit))
			-- and register it			
			if( type(RaidFramesPlus_DebuffBackgroundArray[btn.unit]) == "table") then
			tinsert(RaidFramesPlus_DebuffBackgroundArray[btn.unit],background:GetName())
			else
				RaidFramesPlus_DebuffBackgroundArray[btn.unit] = {background:GetName()}
			end
			-- use the exsiting HealthFontString or create a new one
			local fontString = _G["RaidPullout"..pullout.."Button"..button.."Health"] or 
								_G["RaidPullout"..pullout.."Button"..button.."HealthBar"]:CreateFontString("RaidPullout"..pullout.."Button"..button.."Health",
									"OVERLAY")
								
			if fontString:GetFontObject() == nil then
				fontString:SetFontObject("NumberFontNormalSmall")
			end

			-- now some adjustments
			fontString:SetPoint("CENTER", _G["RaidPullout"..pullout.."Button"..button.."HealthBar"], "BOTTOM", 0, 0)
			-- and something to display
			fontString:SetText(RaidFramesPlus_GetHealthValue(btn.unit))
			-- and do not forget to register this fontString
			if( type(RaidFramesPlus_FontStringArray[btn.unit]) == "table") then
				tinsert(RaidFramesPlus_FontStringArray[btn.unit],fontString:GetName())
			else
				RaidFramesPlus_FontStringArray[btn.unit] = {"RaidPullout"..pullout.."Button"..button.."Health"}
			end
			
			-- RaidTargets
			local targetIcon = btn:CreateTexture("RaidPullout"..pullout.."Button".. button.. "RaidTargetIcon", "OVERLAY")
			targetIcon:SetPoint("TOPRIGHT", _G["RaidPullout"..pullout.."Button"..button.."HealthBar"], "TOPLEFT", 0,0)
			targetIcon:SetWidth(18)
			targetIcon:SetHeight(18)
			targetIcon:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
			local iconIndex = GetRaidTargetIndex(btn.unit)
			if iconIndex then
						SetRaidTargetIconTexture(targetIcon, iconIndex)
						targetIcon:Show()
					else
						targetIcon:Hide()
					end	
			if type (RaidFramesPlus_RaidTargetArray[btn.unit]) == "table" then
				tinsert(RaidFramesPlus_RaidTargetArray[btn.unit], "RaidPullout"..pullout.."Button".. button.. "RaidTargetIcon")
			else
				RaidFramesPlus_RaidTargetArray[btn.unit] = {"RaidPullout"..pullout.."Button".. button.. "RaidTargetIcon"}
			end
			
			-- move the first debuff/buff frame into the pulloutbutton (CT_RAID STYLE)
			 local orgPoint =  _G["RaidPullout"..pullout.."Button"..button.."Aura1"]:GetPoint()
			 local show1 = _G["RaidPullout"..pullout.."Button"..button.."Aura2"].Show
			 local show2 = _G["RaidPullout"..pullout.."Button"..button.."Aura3"].Show
			 local show3 = _G["RaidPullout"..pullout.."Button"..button.."Aura4"].Show



			if RaidFramesPlus_CompactPullouts == 1 then
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:ClearAllPoints()
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:SetPoint("BOTTOMRIGHT", 
					_G["RaidPullout"..pullout.."Button"..button.."HealthBar"], "TOPRIGHT", 0, 2)
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:SetFrameLevel(500)
				
				_G["RaidPullout"..pullout.."Button"..button.."Aura2"]:Hide()
				_G["RaidPullout"..pullout.."Button"..button.."Aura2"].Show = function() end
				
				_G["RaidPullout"..pullout.."Button"..button.."Aura3"]:Hide()
				_G["RaidPullout"..pullout.."Button"..button.."Aura3"].Show = function() end
				
				_G["RaidPullout"..pullout.."Button"..button.."Aura4"]:Hide()
				_G["RaidPullout"..pullout.."Button"..button.."Aura4"].Show = function() end
				
				-- justify Names left (CT_RAID STYLE)
				_G["RaidPullout"..pullout.."Button"..button.."Name"]:SetJustifyH("LEFT")
			else
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:ClearAllPoints()
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:SetPoint(orgPoint)
				
				_G["RaidPullout"..pullout.."Button"..button.."Aura2"].Show = show1
				_G["RaidPullout"..pullout.."Button"..button.."Aura2"]:Show()

				_G["RaidPullout"..pullout.."Button"..button.."Aura3"].Show = show2
				_G["RaidPullout"..pullout.."Button"..button.."Aura3"]:Show()

				_G["RaidPullout"..pullout.."Button"..button.."Aura4"].Show = show3
				_G["RaidPullout"..pullout.."Button"..button.."Aura4"]:Show()

				_G["RaidPullout"..pullout.."Button"..button.."Name"]:SetJustifyH("CENTER")
			end
			
			-- fix borders
			_G["RaidPullout"..pullout.."Button"..button]:SetFrameLevel(_G["RaidPullout"..pullout]:GetFrameLevel()+1)
			_G["RaidPullout"..pullout.."Button"..button.."HealthBar"]:GetStatusBarTexture():SetDrawLayer("BACKGROUND")
			_G["RaidPullout"..pullout.."Button"..button.."ManaBar"]:GetStatusBarTexture():SetDrawLayer("BACKGROUND")
			
			if _G["RaidPullout"..pullout.."Button"..button.."Target"] then
				 _G["RaidPullout"..pullout.."Button"..button.."Target"]:GetStatusBarTexture():SetDrawLayer("BACKGROUND")
			end
						
			if _G["RaidPullout"..pullout.."Button"..button.."Name"] then
				_G["RaidPullout"..pullout.."Button"..button.."Name"]:SetParent(_G["RaidPullout"..pullout.."Button"..button.."HealthBar"])
			end
			
		end
	end
end
--[[
--  RaidFramesPlus_GetHealthValue 
--  TAKES
--   UnitId (uitype:UNIT)
--  RETURNS
--   value (type:string)
--  
--  DESCRIPTION
--   according flag RaidFramesPlus_Health the function format the Healthstatus of UnitId and 
--   return this formatted value
--]]
function RaidFramesPlus_GetHealthValue(UnitId)
	local value = ""
	if RaidFramesPlus_Health == 1 then
		value = UnitHealth(UnitId)
	elseif RaidFramesPlus_Health == 2 then
		value = string.format("%.f%%", UnitHealth(UnitId) / UnitHealthMax(UnitId) * 100)
	elseif RaidFramesPlus_Health == 3 then
		value = UnitHealth(UnitId)
		if value > 1000 then
			value = math.ceil(value / 1000) .. "k"
		end
	elseif RaidFramesPlus_Health == 4 then
		value = UnitHealth(UnitId) - UnitHealthMax(UnitId)
	elseif RaidFramesPlus_Health == 5 and RaidFramesPlus_CustomFormat ~= nil then
		value = RaidFramesPlus_CustomFormat
		for key, func in pairs(RaidFramesPlus_ReplaceTable) do
		
		 value = value:gsub(key, func(UnitId))
		 
		end
    elseif RaidFramesPlus_Health == 5 and RaidFramesPlus_CustomFormat == nil then
		RaidFramesPlus_CustomFormat = "$abk($mak)"
		value = RaidFramesPlus_CustomFormat
		for key, func in pairs(replaceTable) do
		
		 value = value:gsub(key, func(UnitId))
		 
		end
	elseif RaidFramesPlus_Health == 0 then
		value = ""
	end
	return value
end
--[[
--  RaidFramesPlus_UnitHealth 
--  TAKES
--   UnitId (uitype:UNIT)
--  RETURNS
--   nil
--  
--  DESCRIPTION
--   this function scopes in RaidFramesPlus_FontStringArray with given UnitId parameter to 
--   access fontStrings that are associated to the UnitButton in RaidPullouts to set thier text
--   to the formatted value given by RaidFramesPlus_GetHealthValue
--
--  SEE ALSO
--   RaidFramesPlus_GetHealthValue
--   RaidFramesPlus_UnitThreatSituationUpdate
--]]
function RaidFramesPlus_UnitHealth(UnitId)
	-- check the probably populated Array for an associated fontString
	local FontStringArray = RaidFramesPlus_FontStringArray[UnitId]
	if FontStringArray == {} or FontStringArray == nil then 
		return 
	end
	for i, fontString in pairs(FontStringArray) do
		if fontString then
			-- and change the Text to the current Health Amount
			local value = RaidFramesPlus_GetHealthValue(UnitId)
			
			--ChatFrame1:AddMessage(value)
			_G[fontString]:SetText(value)
		end
	end
end
--[[
--  RaidFramesPlus_VariablesLoaded 
--  TAKES
--   nil
--  RETURNS
--   nil
--  
--  DESCRIPTION
--   this function acts as DefaultVariables-Loader / SavedVariables-Loader
--]]
local function RaidFramesPlus_VariablesLoaded()
	if RaidFramesPlus_Health == nil then
		RaidFramesPlus_Health = 1
	end
	if RaidFramesPlus_CompactPullouts == nil then
		RaidFramesPlus_CompactPullouts = 1
	end
	if RaidFramesPlus_ColorizeNames == nil then
		RaidFramesPlus_ColorizeNames = 1
	end
	if  RaidFramesPlus_HideTargetFrameNameBackground == nil then
		RaidFramesPlus_HideTargetFrameNameBackground = 1
	end
	if RaidFramesPlus_ShowRaidTargetIcons == nil then
		RaidFramesPlus_ShowRaidTargetIcons = 1
	end
end 
--[[
--  RaidFramesPlus_ToggleBuffs
--  TAKES
--   nil
--  RETURNS
--   nil
--
--  DESCRIPTION
--   this function mass toggle the display of buffs/debuffs on all RaidPullouts
--]]
local function RaidFramesPlus_ToggleBuffs()
	for index = 1, NUM_RAID_PULLOUT_FRAMES do
		local pullout = _G["RaidPullout"..index]
		if RaidFramesPlus_ShowBuffs then
			pullout.showBuffs = nil
		else
			pullout.showBuffs = 1
		end
		RaidPullout_Update(pullout)
	end
	if RaidFramesPlus_ShowBuffs then
		RaidFramesPlus_ShowBuffs = nil
	else
		RaidFramesPlus_ShowBuffs = 1
	end
end 
--[[
--  RaidFramesPlus_ParseSlashCommand
--  TAKES
--   argv(type:string)
--  RETURNS
--   nil
--  
--  DESCRIPTION @OBSOLETE
--   this function parse the given arguments that passed to the /slashcommand and set
--   associcated flags
--
--  DESCRIPTION
--   scan argv for string -d to toggle between buffs/debuffs
--]]
local function RaidFramesPlus_ParseSlashCommand(argv)
	if string.find(string.lower(argv), "-d") then
		RaidFramesPlus_ToggleBuffs()
	end
end 
--[[
--  RaidFramesPlus_CreateOptionPanel
--  TAKES
--   nil
--  RETURNS
--   nil
--  
--  DESCRIPTION
--   Create a graphical configuration interface
--]]
local function RaidFramesPlus_CreateOptionPanel()
	local setupPanel = CreateFrame("FRAME", "RaidFramesPlus_SetupPanel")
	setupPanel.name = "RaidFramesPlus"
	
	local title = setupPanel:CreateFontString("RaidFramesPlus_Title")
	title:SetPoint("TOPLEFT", 16, -16)
	title:SetFontObject(GameFontNormalLarge)
	title:SetText("RaidFramesPlus")
	local descriptionText = setupPanel:CreateFontString("RaidFramesPlus_DescriptionText")
	descriptionText:SetFontObject(GameFontHighlightSmall)
	descriptionText:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
	descriptionText:SetPoint("RIGHT", -30, 0)
	descriptionText:SetHeight(32)
	descriptionText:SetJustifyH("LEFT")
	descriptionText:SetJustifyV("TOP")
	descriptionText:SetText("Customize the RaidFrames to your needs, see also Game-properties > Unitframes to customize buff/debuff display on RaidFrames ")
	
	local colorizeCheckButton = CreateFrame("CheckButton", "RaidFramesPlus_ColorizeCheckButton", setupPanel ,"OptionsCheckButtonTemplate")
	RaidFramesPlus_ColorizeCheckButtonText:SetText("|cFFFFFFFFColorize Names|r")
	colorizeCheckButton:SetPoint("TOPLEFT", descriptionText, "BOTTOMLEFT", 0, -8)
	colorizeCheckButton:SetChecked(RaidFramesPlus_ColorizeNames)
	
	local raidTargetIconCheckButton = CreateFrame("CheckButton", "RaidFramesPlus_RaidTargetIconCheckButton", setupPanel, "OptionsCheckButtonTemplate")
	RaidFramesPlus_RaidTargetIconCheckButtonText:SetText("|cFFFFFFFFRaidtarget Icons|r")
	raidTargetIconCheckButton:SetPoint("TOPLEFT", colorizeCheckButton, "TOPLEFT", 200, 0)
	raidTargetIconCheckButton:SetChecked(RaidFramesPlus_ShowRaidTargetIcons) 
	
	local targetFrameCheckButton = CreateFrame("CheckButton", "RaidFramesPlus_TargetFrameCheckButton", setupPanel, "OptionsCheckButtonTemplate")
	RaidFramesPlus_TargetFrameCheckButtonText:SetText("|cFFFFFFFFHide Targetframebg.|r")
	targetFrameCheckButton:SetPoint("TOPLEFT", colorizeCheckButton, "BOTTOMLEFT", 0, -8)
	targetFrameCheckButton:SetChecked(RaidFramesPlus_HideTargetFrameNameBackground)
	
	local compactCheckButton = CreateFrame("CheckButton", "RaidFramesPlus_CompactCheckButton", setupPanel, "OptionsCheckButtonTemplate")
	RaidFramesPlus_CompactCheckButtonText:SetText("|cFFFFFFFFCompact Display|r")
	compactCheckButton:SetPoint("TOPLEFT",raidTargetIconCheckButton, "BOTTOMLEFT", 0, -8)
	compactCheckButton:SetChecked(RaidFramesPlus_CompactPullouts)
	
	local healthText0CheckButton = CreateFrame("CheckButton", "RaidFramesPlus_HealthText0CheckButton", setupPanel, "OptionsCheckButtonTemplate")
	RaidFramesPlus_HealthText0CheckButtonText:SetText("|cFFFFFFFFDisplay Healthvalues in |r")
	healthText0CheckButton:SetChecked(not (RaidFramesPlus_Health == 0))
	
	healthText0CheckButton:SetScript("OnClick", function(self)
		if self:GetChecked() then
			RaidFramesPlus_HealthText2CheckButton:Enable()
			RaidFramesPlus_HealthText3CheckButton:Enable()
			RaidFramesPlus_HealthText4CheckButton:Enable()
			RaidFramesPlus_HealthText5CheckButton:Enable()
		else
			RaidFramesPlus_HealthText2CheckButton:Disable()
			RaidFramesPlus_HealthText3CheckButton:Disable()
			RaidFramesPlus_HealthText4CheckButton:Disable()
			RaidFramesPlus_HealthText5CheckButton:Disable()
		end
	end)
	healthText0CheckButton:SetPoint("TOPLEFT", targetFrameCheckButton, "BOTTOMLEFT", 0, -8)
	
	local healthText1CheckButton = CreateFrame("CheckButton", "RaidFramesPlus_HealthText1CheckButton", setupPanel, "OptionsSmallCheckButtonTemplate")
	RaidFramesPlus_HealthText1CheckButtonText:SetText("|cFFFFFFFFAbsolute Values|r")
	healthText1CheckButton:SetChecked(RaidFramesPlus_Health == 1)
	healthText1CheckButton:SetScript("OnClick", function(self)
		if self:GetChecked() then
			RaidFramesPlus_HealthText2CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText3CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText4CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText5CheckButton:SetChecked(false)
		end
	end)
	
	healthText1CheckButton:SetPoint("TOPLEFT", healthText0CheckButton, "BOTTOMLEFT", 8, -8)
	
	
	local healthText2CheckButton = CreateFrame("CheckButton", "RaidFramesPlus_HealthText2CheckButton", setupPanel, "OptionsSmallCheckButtonTemplate")
	RaidFramesPlus_HealthText2CheckButtonText:SetText("|cFFFFFFFFPercent|r")
	healthText2CheckButton:SetChecked(RaidFramesPlus_Health == 2)
	healthText2CheckButton:SetScript("OnClick", function(self)
		if self:GetChecked() then
			RaidFramesPlus_HealthText1CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText3CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText4CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText5CheckButton:SetChecked(false)
		end
	end)

	healthText2CheckButton:SetPoint("TOPLEFT", healthText1CheckButton, "BOTTOMLEFT", 0, -8)
	
	local healthText3CheckButton = CreateFrame("CheckButton", "RaidFramesPlus_HealthText3CheckButton", setupPanel, "OptionsSmallCheckButtonTemplate")
	RaidFramesPlus_HealthText3CheckButtonText:SetText("|cFFFFFFFFK-Values|r")
	healthText3CheckButton:SetChecked(RaidFramesPlus_Health == 3)
	healthText3CheckButton:SetScript("OnClick", function(self)
		if self:GetChecked() then
			RaidFramesPlus_HealthText1CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText2CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText4CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText5CheckButton:SetChecked(false)
		end
	end)
	healthText3CheckButton:SetPoint("TOPLEFT", healthText2CheckButton, "BOTTOMLEFT", 0, -8)
	
	local healthText4CheckButton = CreateFrame("CheckButton", "RaidFramesPlus_HealthText4CheckButton", setupPanel, "OptionsSmallCheckButtonTemplate")
	RaidFramesPlus_HealthText4CheckButtonText:SetText("|cFFFFFFFFDeficite|r")
	healthText4CheckButton:SetChecked(RaidFramesPlus_Health == 4)
	healthText4CheckButton:SetScript("OnClick", function(self)
		if self:GetChecked() then
			RaidFramesPlus_HealthText1CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText2CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText3CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText5CheckButton:SetChecked(false)
		end
	end)

	healthText4CheckButton:SetPoint("TOPLEFT", healthText3CheckButton, "BOTTOMLEFT", 0, -8)
	
	local healthText5CheckButton = CreateFrame("CheckButton", "RaidFramesPlus_HealthText5CheckButton", setupPanel, "OptionsSmallCheckButtonTemplate")
	healthText5CheckButton:SetChecked(RaidFramesPlus_Health == 5)
	RaidFramesPlus_HealthText5CheckButtonText:SetText("|cFFFFFFFFCustom Format|r")
	healthText5CheckButton:SetScript("OnClick", function(self)
		if self:GetChecked() then
			RaidFramesPlus_HealthText1CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText2CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText3CheckButton:SetChecked(false)
			RaidFramesPlus_HealthText4CheckButton:SetChecked(false)
		end
	end)

	healthText5CheckButton:SetPoint("TOPLEFT", healthText4CheckButton, "BOTTOMLEFT", 0, -8)
	
	local healthTextCustomEditBox = CreateFrame("EditBox", "RaidFramesPlus_HealthTextCustomEditBox", setupPanel, "InputBoxTemplate")
	healthTextCustomEditBox:SetPoint("TOPLEFT", healthText5CheckButton, "BOTTOMLEFT", 16, -8)
	healthTextCustomEditBox:SetWidth(256)
	healthTextCustomEditBox:SetHeight(24)
	
	healthTextCustomEditBox:SetAutoFocus(false)
	healthTextCustomEditBox:ClearFocus()
	
	setupPanel.default = function()
		RaidFramesPlus_Health = 1
		RaidFramesPlus_CompactPullouts = 1
		RaidFramesPlus_ColorizeNames = 1
		RaidFramesPlus_HideTargetFrameNameBackground = 1
		RaidFramesPlus_ColorizeCheckButton:SetChecked(1)
		RaidFramesPlus_CompactCheckButton:SetChecked(1)
		RaidFramesPlus_TargetFrameCheckButton:SetChecked(1)
		
		RaidFramesPlus_HealthText0CheckButton:SetChecked(1)
		RaidFramesPlus_HealthText1CheckButton:SetChecked(1)
		RaidFramesPlus_HealthText2CheckButton:SetChecked(nil)
		RaidFramesPlus_HealthText3CheckButton:SetChecked(nil)
		RaidFramesPlus_HealthText4CheckButton:SetChecked(nil)
		RaidFramesPlus_HealthText5CheckButton:SetChecked(nil)
		RaidFramesPlus_RaidTargetIconCheckButton:SetChecked(1)
	end
	
	setupPanel.cancel = function()
		RaidFramesPlus_ColorizeCheckButton:SetChecked(RaidFramesPlus_ColorizeNames)
		RaidFramesPlus_CompactCheckButton:SetChecked(RaidFramesPlus_CompactPullouts)
		RaidFramesPlus_TargetFrameCheckButton:SetChecked(RaidFramesPlus_HideTargetFrameNameBackground)
		
		if RaidFramesPlus_Health > 0 then
			RaidFramesPlus_HealthText0CheckButton:SetChecked(1)
		end
		
		RaidFramesPlus_HealthText1CheckButton:SetChecked(RaidFramesPlus_Health == 1)
		RaidFramesPlus_HealthText2CheckButton:SetChecked(RaidFramesPlus_Health == 2)
		RaidFramesPlus_HealthText3CheckButton:SetChecked(RaidFramesPlus_Health == 3)
		RaidFramesPlus_HealthText4CheckButton:SetChecked(RaidFramesPlus_Health == 4)
		RaidFramesPlus_HealthText5CheckButton:SetChecked(RaidFramesPlus_Health == 5)
		RaidFramesPlus_HealthTextCustomEditBox:SetEnabled(RaidFramesPlus_Health == 5)
		RaidFramesPlus_RaidTargetIconCheckButton:SetChecked(RaidFramesPlus_ShowRaidTargetIcons == 1)
	end	
	
	setupPanel.okay = function()
		RaidFramesPlus_ColorizeNames = RaidFramesPlus_ColorizeCheckButton:GetChecked()
		RaidFramesPlus_CompactPullouts = RaidFramesPlus_CompactCheckButton:GetChecked()
		RaidFramesPlus_HideTargetFrameNameBackground = RaidFramesPlus_TargetFrameCheckButton:GetChecked()
		
		RaidFramesPlus_ShowRaidTargetIcons = RaidFramesPlus_RaidTargetIconCheckButton:GetChecked()
		
		if RaidFramesPlus_HealthText0CheckButton:GetChecked() == nil then
			RaidFramesPlus_Health = 0
		end
		if RaidFramesPlus_HealthText1CheckButton:GetChecked() then
			RaidFramesPlus_Health = 1
		end
		if RaidFramesPlus_HealthText2CheckButton:GetChecked() then
			RaidFramesPlus_Health = 2
		end
		if RaidFramesPlus_HealthText3CheckButton:GetChecked() then
			RaidFramesPlus_Health = 3
		end
		if RaidFramesPlus_HealthText4CheckButton:GetChecked() then
			RaidFramesPlus_Health = 4
		end
		if RaidFramesPlus_HealthText5CheckButton:GetChecked() then
			RaidFramesPlus_Health = 5
			RaidFramesPlus_CustomFormat = RaidFramesPlus_HealthTextCustomEditBox:GetText()
		end
		RaidFramesPlus_RaidRosterUpdate()
		RaidFramesPlus_PartyMemberChanged()
		RaidFramesPlus_PlayerTargetChanged()
	end

	InterfaceOptions_AddCategory(setupPanel)  
end
 
local function RaidFramesPlus_EventHandler(self, event, arg1)
	if event == "UNIT_THREAT_SITUATION_UPDATE" then
		RaidFramesPlus_UnitThreatSituationUpdate(arg1)
	elseif event == "UNIT_HEALTH" then
		RaidFramesPlus_UnitHealth(arg1)
	elseif event == "PLAYER_TARGET_CHANGED" then
		RaidFramesPlus_PlayerTargetChanged()
	elseif event == "RAID_ROSTER_UPDATE" then
		RaidFramesPlus_RaidRosterUpdate()
	elseif event == "PARTY_MEMBER_CHANGED" then
		RaidFramesPlus_PartyMemberChanged()
	elseif event == "UNIT_AURA" then
		RaidFramesPlus_UnitAura(UnitId)
	elseif event == "VARIABLES_LOADED" then
		RaidFramesPlus_VariablesLoaded()
	elseif event == "RAID_TARGET_UPDATE" then
		RaidFramesPlus_RaidTargetUpdate(arg1)
	end
end
local function RaidFramesPlus_RegisterEvents()
	RaidFramesPlus_IsEnabled = true
	RaidFramesPlus:RegisterEvent("UNIT_THREAT_SITUATION_UPDATE")
	RaidFramesPlus:RegisterEvent("UNIT_HEALTH")
	RaidFramesPlus:RegisterEvent("RAID_ROSTER_UPDATE")
	RaidFramesPlus:RegisterEvent("PLAYER_TARGET_CHANGED")
	RaidFramesPlus:RegisterEvent("PARTY_MEMBER_CHANGED")
	RaidFramesPlus:RegisterEvent("UNIT_AURA")
	RaidFramesPlus:RegisterEvent("RAID_TARGET_UPDATE")
	RaidFramesPlus:RegisterEvent("VARIABLES_LOADED")
	-- run once
	RaidFramesPlus_PlayerTargetChanged()
	RaidFramesPlus_PartyMemberChanged()
	RaidFramesPlus_CreateOptionPanel()
	-- eliminate this
	RaidFramesPlus:SetScript("OnUpdate", nil)
end

RaidFramesPlus:SetScript("OnUpdate", RaidFramesPlus_RegisterEvents)
RaidFramesPlus:SetScript("OnEvent", RaidFramesPlus_EventHandler)

SlashCmdList["RAIDFRAMESPLUS"] = RaidFramesPlus_ParseSlashCommand
SLASH_RAIDFRAMESPLUS1 = "/rfp"